﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using Maticsoft.Common;
using Newtonsoft.Json;
using System.Data;
using Newtonsoft.Json.Converters;

namespace jmwqz.ajax
{
    /// <summary>
    /// duihuan 的摘要说明
    /// </summary>
    public class duihuan : IHttpHandler, IRequiresSessionState
    {
        protected int totalCount;
        protected int page;
        protected int pageSize;

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string _action = MyCommFun.QueryString("myact");
            if (_action == "getlist")
            {
                if (context.Session["userid"] == null)
                {
                    context.Response.Write("2");
                    return;
                }
                Maticsoft.BLL.tb_duihuan bitem = new Maticsoft.BLL.tb_duihuan();

                string strsql = "select dh.inquiretime,ud.realname,i.name,i.img_thumb,i.price,i.cost,dh.id,dh.state,dh.inquiretime,uds.* from tb_duihuan dh " + 
"left JOIN  tb_item i on dh.itemid = i.id " +
"left JOIN  tb_userdata ud on ud.id=dh.uid " +
"LEFT JOIN ( SELECT wid,addrdetail,tel,contractPerson,pc3. NAME as province , pc2. NAME as city , pc1. NAME as area " +
"FROM wx_shop_user_addr wsu LEFT JOIN pre_common_district pc1 ON pc1.id = wsu.area " +
"LEFT JOIN pre_common_district pc2 ON pc1.upid = pc2.id " +
"LEFT JOIN pre_common_district pc3 ON pc2.upid = pc3.id) as uds on uds.wid=ud.id where dh.uid=" + context.Session["userid"] + " order by dh.inquiretime desc";

                DataSet ds = Maticsoft.DBUtility.DbHelperMySQL.Query(strsql);
                if (!Maticsoft.Common.Util2.CheckDataSet(ds))
                {
                    context.Response.Write("1");
                }
                else
                {
                    List<exclass> das = new List<exclass>(); 
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++ )
                    {
                        exclass model = new exclass();  
                        if (ds.Tables[0].Rows[i]["id"] != null && ds.Tables[0].Rows[i]["id"].ToString() != "")
                        {
                            model.id = int.Parse(ds.Tables[0].Rows[i]["id"].ToString());
                        }
                        if (ds.Tables[0].Rows[i]["img_thumb"] != null)
                        {
                            model.img_thumb = ds.Tables[0].Rows[i]["img_thumb"].ToString();
                        }
                        if (ds.Tables[0].Rows[i]["inquiretime"] != null)
                        {
                            model.inquiretime = Convert.ToDateTime(ds.Tables[0].Rows[i]["inquiretime"].ToString()).ToString("yyyy-MM-dd HH");
                        }
                        if (ds.Tables[0].Rows[i]["price"] != null)
                        {
                            model.price = ds.Tables[0].Rows[i]["price"].ToString();
                        }
                        if (ds.Tables[0].Rows[i]["cost"] != null)
                        {
                            model.cost = ds.Tables[0].Rows[i]["cost"].ToString();
                        }
                        if (ds.Tables[0].Rows[i]["state"] != null)
                        {
                            model.state = ds.Tables[0].Rows[i]["state"].ToString();
                        }
                        if (ds.Tables[0].Rows[i]["name"] != null)
                        {
                            model.name = ds.Tables[0].Rows[i]["name"].ToString();
                        }
                        das.Add(model);
                    }
                    context.Response.Write(JsonConvert.SerializeObject(das));
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public class exclass
        {
            public exclass()
            {
                
            }
            public string img_thumb { get; set; }
            public string inquiretime { get; set; }
            public string price { get; set; }
            public string cost { get; set; }
            public string state { get; set; }
            public int id { get; set; }
            public string name { get; set; }
        }
    }
}